package com.ruoyi.web.controller.dr;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.dr.domain.DrSellDetails;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dr.domain.DrSampledata;
import com.ruoyi.dr.service.IDrSampledataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 寄样Controller
 * 
 * @author ruoyi
 * @date 2025-07-14
 */
@RestController
@RequestMapping("/dr/sampledata")
public class DrSampledataController extends BaseController
{
    @Autowired
    private IDrSampledataService drSampledataService;

    /**
     * 查询寄样列表
     */
//    @PreAuthorize("@ss.hasPermi('system:sampledata:list')")
    @GetMapping("/list")
    public TableDataInfo list(DrSampledata drSampledata) {
        startPage();
        List<DrSampledata> list = drSampledataService.selectDrSampledataList(drSampledata);
        return getDataTable(list);
    }

    /**
     * 导出寄样列表
     */
//    @PreAuthorize("@ss.hasPermi('system:sampledata:export')")
    @Log(title = "寄样", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DrSampledata drSampledata)
    {
        List<DrSampledata> list = drSampledataService.selectDrSampledataList(drSampledata);
        ExcelUtil<DrSampledata> util = new ExcelUtil<DrSampledata>(DrSampledata.class);
        util.exportExcel(response, list, "寄样数据");
    }

    /**
     * 获取寄样详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:sampledata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(drSampledataService.selectDrSampledataById(id));
    }

    /**
     * 新增寄样
     */
//    @PreAuthorize("@ss.hasPermi('system:sampledata:add')")
    @Log(title = "寄样", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DrSampledata drSampledata)
    {
        return toAjax(drSampledataService.insertDrSampledata(drSampledata));
    }

    /**
     * 修改寄样
     */
//    @PreAuthorize("@ss.hasPermi('system:sampledata:edit')")
    @Log(title = "寄样", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DrSampledata drSampledata)
    {
        return toAjax(drSampledataService.updateDrSampledata(drSampledata));
    }

    /**
     * 删除寄样
     */
//    @PreAuthorize("@ss.hasPermi('system:sampledata:remove')")
    @Log(title = "寄样", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(drSampledataService.deleteDrSampledataByIds(ids));
    }

    /**
     * 寄样分析
     */
	@GetMapping("/sample/sell/analysis")
    public AjaxResult sampleSellAnalysis() {
        LoginUser loginUser = this.getLoginUser();
        return success(drSampledataService.sampleSellAnalysis(loginUser.getUsername()));
    }



    @Log(title = "寄样数据导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<DrSampledata> util = new ExcelUtil<>(DrSampledata.class);
        List<DrSampledata> sampledata = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = drSampledataService.importSampledata(sampledata, operName);
        return success(message);
    }
}
